package logic

import (
	"context"
	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/doctorrpc/internal/svc"
	"doctor-medicine-system/models/mysql"
	"doctor-medicine-system/pkg"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *doctorrpc.UserLoginRequest) (*doctorrpc.UserLoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User

	if user.ID == 0 {
		return nil, errors.New("用户不存在")
	}

	if pkg.Md5(in.Password) != user.Password {
		return nil, errors.New("密码不正确，请重新输入")
	}

	err := user.FindUserLoginBy(mysql.DB, in.Mobile)
	if err != nil {
		return nil, errors.New("登入失败")
	}

	return &doctorrpc.UserLoginResponse{
		UserId: int64(user.ID),
	}, nil
}
